home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / HQX 1.0 / ReadMe < prev   
Encoding:
Text File  |  1997-07-05  |  12.4 KB  |  277 lines  |  [ttro/ttxt]

  1. HQX 1.0
  2. BinHex decoder/encoder routines, ReadMe file.
  3. Copyright (c) 1995, 1996, 1997 by John Montbriand.  All Rights Reserved.
  4. Permission granted for public use.
  5. Distribute freely in areas where the laws of copyright apply.
  6. USE AT YOUR OWN RISK.
  7. DO NOT DISTRIBUTE MODIFIED COPIES.
  8. Comments/questions/postcards* to the author at the address:
  9.       John Montbriand
  10.       P.O. Box. 1133
  11.       Saskatoon Saskatchewan Canada
  12.       S7K 3N2
  13. or by email at:
  14.       tinyjohn@sk.sympatico.ca
  15. *if you mail a postcard, then I will provide you with technical support regarding questions you may have about this file.
  16.  
  17.  
  18.  
  19. About BinHex decoder/encoder routines...
  20.  
  21. These routines implement BinHex encoding and decoding in a input/output source/destination independent way.  Complete source code is provided along with a MPW tool example.  Usable in System 6 on up...  Can handle any file size.  Uses a chained read/write algorithm so memory requirements are low.
  22.  
  23.  
  24.  
  25. Sections found below
  26.  
  27.     Input and Output
  28.         A Data Sink
  29.             EXAMPLE
  30.         A Data Source
  31.             EXAMPLE
  32.             SPECIAL NOTE ABOUT DECODING DATA
  33.     Naming Decoded Files
  34.         EXAMPLE
  35.     Encoding BinHex Files
  36.         EXAMPLE
  37.     Decoding BinHex Files
  38.         EXAMPLE
  39.     Multi-part files
  40.     Files in this package
  41.     Copies for Sale!
  42.     NO WARRANTY
  43.     Bug reports make this a better product!
  44.     Other
  45.  
  46.  
  47.  
  48. Input and Output
  49.  
  50. Input and Output are handled by callback routines that you provide.  In your callbacks it is your responsibility to either retrieve data or store the data.  Callbacks you provide are as follows:
  51.  
  52.  
  53. A Data Sink
  54.  
  55. This routine accepts data produced by the BinHex encoder.  If you are only interested in Encoding BinHex files, then this is the only routine you have to implement.  It is declared as:
  56.  
  57.     typedef OSErr (*HQXSink)(void* buffer, long count, long refcon);
  58.  
  59.    • buffer is a pointer to the BinHex data.
  60.    • count is the number of bytes in the data buffer.
  61.    • refcon is same as the refcon parameter passed to the HQXEncode routine.
  62.  
  63. HQXSink defines a function that will receive data produced by the HQXEncode routine.  refcon is  available for your application's use and is the same refcon value passed to HQXEncode.  returning any error code other than noErr will abort HQXEncode.  HQXEncode implements a buffering scheme of its own, so there is no need for your sink function to be concerned about buffering data.
  64.  
  65. EXAMPLE:  Here's an example using an open Macintosh file:
  66.  
  67. short gFileRefNum ; /* an open Macintosh file */
  68.  
  69. OSErr MySink(void* buffer, long count, long refcon) {
  70.        return FSWrite(gFileRefNum, &count, buffer);
  71. }
  72.  
  73.  
  74. A Data Source
  75.  
  76. This routine is called by the Decoder function whenever it requires more BinHex data during the decoding process.  If you are only interested in Decoding BinHex files, then this is the only routine you have to implement.  It is declared as:
  77.  
  78.    typedef OSErr (*HQXSource)(void* buffer, long *count, long refcon);
  79.  
  80.    • buffer is a pointer a buffer that has been prepared to received.
  81.    • *count is the number of bytes being requested.  Before returning, your source function should set *count to the number of bytes actually read into the data buffer (any value larger than zero is will continue processing.  a value of zero will abort processing).
  82.    • refcon is same as the refcon parameter passed to the HQXEncode routine.
  83.  
  84. HQXSource defines a function providing a data source for the HQXDecode routine.  Your function should attempt to read *count bytes to the buffer.  Before returning, your function should set *count to the actual number of bytes read into the buffer.  If count is zero, or the source function returns some result other than eofErr, HQXDecode aborts processing.  HQXDecode implements its own buffering scheme so your source function need not be concerned about buffering.
  85.  
  86.  
  87. EXAMPLE:  Here's an example using an open Macintosh file:
  88.  
  89. short gFileRefNum; /* an open Macintosh file */
  90.  
  91. OSErr MySource(void* buffer, long *count, long refcon) {
  92.        return FSRead(gFileRefNum, count, buffer);
  93. }
  94.  
  95.  
  96. SPECIAL NOTE ABOUT DECODING DATA: Only BinHex characters delimited by colons are converted and multiple BinHex data sections are allowed.  For example,  the data sequence:
  97.  
  98.     :"@KaH#jS!:     ...some garbage characters...  :&4&@&40:
  99.  
  100. is processed the same as the BinHex sequence:
  101.  
  102.     :"@KaH#jS!&4&@&40:
  103.  
  104. In other words, colons in the input stream can be used to toggle BinHex decoding on and off.
  105.  
  106.  
  107.  
  108. Naming Decoded Files
  109.  
  110. The name of the produced file is determined from the BinHex data during the first part of the decoding process.  By default, a file with this name is created in the current directory.  If you would like to override this behavior, then you can provide an optional name filter function allowing you to modify the name and location of the decoded file.  This function is declared as:
  111.  
  112.    typedef OSErr (*HQXNameFilter)(StringPtr name, short *vol, long *dir, long refcon);
  113.    
  114.    • name,  is the name of the file extracted from the BinHex information.
  115.    • *vol and *dir are initially set to zero, but can be modified to refer to any valid directory.
  116.    • refcon is same as the refcon parameter passed to the HQXDecode routine.
  117.  
  118. HQXNameFilter is an optional routine allowing your application opportunity to modify the file name and location of the macintosh file being decoded.  If your filter function returns a non-zero result code, HQXDecode stops processing data from the source and returns the result code.  when HQXNameFilter is called, name is the name of the file and *vol and *dir will be zero.  Your routine can modify any or all of these parameters to specify a different output file location.
  119.  
  120.  
  121. EXAMPLE:  Here's an example using the Standard File package to place the file:
  122.  
  123. OSErr MyNameFilter(StringPtr name, short *vol, long *dir, long refcon) {
  124.     StandardFileReply reply; 
  125.     StandardPutFile ("\pSave As:", name, &reply);
  126.     if (reply.sfGood) {
  127.         PLstrcpy(name, reply.sfFile.name);
  128.         *vol = reply.sfFile.vRefNum;
  129.         *dir = reply.sfFile.parID;
  130.     } else return userCanceledErr;
  131. }
  132.  
  133.  
  134.  
  135. Encoding BinHex Files
  136.  
  137. The routine HQXEncode encodes a macintosh file in BinHex format sending the produced output to the data sink routine you provide as a parameter.  This routine is defined as:
  138.  
  139.    OSErr HQXEncode(StringPtr name, short vol, long dir, HQXSink dst, long refcon);
  140.  
  141.    • name,  vol, and dir refer to the macintosh file's disk location.
  142.    • dst is the destination data sink where encoded output is sent
  143.    • refcon is a parameter for your own use that is passed along to your sink function
  144.  
  145. HQXEncode reads the indicated macintosh file and outputs the BinHex equivalent to the sink.  If the HQXSink returns a non-zero result code, then execution stops and HQXEncode will return the result.  refcon is a value passed to the sink function.  HQXEncode implements its own internal buffering scheme so your sink function need not be concerned about buffering data.
  146.  
  147.  
  148. EXAMPLE:  Here's an example of how to encode a file using the Standard File package and the MySink function defined above.  The result is saved in a MPW text file:
  149.  
  150. OSErr EncodeAFile(void) {
  151.     SFTypeList typeList = {'TEXT', 'TEXT', 'TEXT', 'TEXT'};
  152.     StandardFileReply inputfile, outputfile;
  153.     Boolean file_exists;
  154.     OSErr err;
  155.     
  156.     gFileRefNum = 0;
  157.     file_exists = false;
  158.     
  159.     StandardGetFile(NULL, 1, typeList, &inputfile);
  160.     if (!inputfile.sfGood) { err = userCanceledErr; goto bail; }
  161.     StandardPutFile ("\pSave BinHex As:", name, &outputfile);
  162.     if (!outputfile.sfGood) { err = userCanceledErr; goto bail; }
  163.     if (outputfile.sfReplacing) FSpDelete(&outputfile.sfFile);
  164.     err = FSpCreate(&outputfile.sfFile, 'MPS ', fileType, smSystemScript);
  165.     if (err != noErr) goto bail;
  166.     file_exists = true;
  167.     err = FSpOpenDF(&outputfile.sfFile, fsRdWrPerm, &gFileRefNum);
  168.     if (err != noErr) goto bail;
  169.     err = HQXEncode(inputfile.sfFile.name, inputfile.sfFile.vRefNum,
  170.             inputfile.sfFile.parID, MySink, 0);
  171.     if (err != noErr) goto bail;
  172.     FSClose(gFileRefNum);
  173.     return noErr;
  174. bail:
  175.     if (gFileRefNum != 0) FSClose(gFileRefNum);
  176.     if (file_exists) FSpDelete(&outputfile.sfFile);
  177.     return err;
  178. }
  179.  
  180. EXAMPLE:  Here's an example of how to encode a file using the standard file package and the MySink function defined above.  The result is saved in a MPW text file:
  181.  
  182.  
  183.  
  184. Decoding BinHex Files
  185.  
  186. The routine HQXDecode decodes BinHex data retrieved via. your source function and produces a macintosh file.  The routine is defined as follows:
  187.  
  188.    OSErr HQXDecode(HQXSource src, HQXNameFilter fname, Boolean can_replace, Boolean header_search, long refcon);
  189.  
  190.    • src is the data source function you provide.
  191.    • fname is the name filter function you provide.  If you want the file to be created in the current directory using the name from the BinHex data, then this parameter may be set to NULL.
  192.    • if can_replace is true, then if a file already exists with the same name as the output file, it will be replaced.
  193.    • if header_search is true, then the decoder searches for the string "(This file must be converted with BinHex 4.0)" in the input stream and does not start processing BinHex data until it is found.
  194.    • refcon is a parameter for your own use that is passed along to your source function
  195.  
  196. HQXDecode decodes a BinHex file using data from the source function.  fname, if not NULL, is called once the file name has been extracted from the BinHex data so you can change where the file is saved.  if can_replace is true then decoded files will replace files with the same name.  refcon is an application-specific parameter passed to both the name filter function and the source function.  if header_search is true, the routine does a search for the string "(This file must be converted with BinHex 4.0)" before beginning decoding BinHex data.
  197.  
  198.  
  199. EXAMPLE:  Here's an example of how to decode a file using the Standard File package and both the MySource and the MyNameFilter functions defined above.
  200.  
  201. OSErr DecodeAFile(void) {
  202.     SFTypeList typeList = {'TEXT', 'TEXT', 'TEXT', 'TEXT'};
  203.     StandardFileReply inputfile;
  204.     OSErr err;
  205.     
  206.     gFileRefNum = 0;
  207.     
  208.     StandardGetFile(NULL, 1, typeList, &inputfile);
  209.     if (!inputfile.sfGood) { err = userCanceledErr; goto bail; }
  210.     err = FSpOpenDF(&inputfile.sfFile, fsRdPerm, &gFileRefNum);
  211.     if (err != noErr) goto bail;
  212.     err = HQXDecode(MySource, MyNameFilter, true, true, 0);
  213.     if (err != noErr) goto bail;
  214.     FSClose(gFileRefNum);
  215.     return noErr;
  216. bail:
  217.     if (gFileRefNum != 0) FSClose(gFileRefNum);
  218.     return err;
  219. }
  220.  
  221.  
  222.  
  223. Multi-part files
  224.  
  225. Obviously, since these routines are input independent, they do not have access multiple BinHex files during the decoding or encoding process.  Handling of multi-part files is a feature you can easily build into your Sink and Source routines.
  226.  
  227.  
  228.  
  229. Files in this package
  230.  
  231. hqx.c -- source code for the BinHex routine's implementation
  232. hqx.h -- header file providing interfaces to the BinHex routines
  233. ReadMe  -- this file
  234.  
  235. :example: -- MPW example Tool
  236. :obj: -- object files made during compile
  237. hqx.c -- the hqx library file
  238. hqx.h -- the hqx library interface file
  239. hqxtool -- a compiled version of the tool
  240. hqxtool.c  -- tool source code
  241. hqxtool.r  -- rez file for the tool
  242. hqxtool.rsrc -- resource file for the tool
  243. MakeFile -- make command file, builds the tool
  244.  
  245. :Libraries: -- precompiled libraries, ready to link.  See :example:MakeFile for the compiler options used.
  246. hqx.c.o -- 68K version of the hqx library
  247. hqx.xcoff -- PowerPC version of the hqx library
  248.  
  249.  
  250.  
  251. Copies for Sale!
  252.  
  253. These libraries are provided for free and you may use them in any program you make;  however, if you would like to purchase a copy of these libraries and have a legal paper-trail establishing your right to use them, then send along a cheque or a money order in the amount of $10.00 for the purchase of one copy and I'll send you a receipt.
  254.  
  255.  
  256.  
  257. NO WARRANTY
  258.  
  259. No warranties are made regarding these files.  John Montbriand disclaims all warranties regarding these files, either express or implied, including but not limited to implied warranties of merchantability and fitness for any particular purpose.  These files are provided "AS IS" without any warranty of any kind.  Use them at your own risk.  Copies of these files are not for sale in areas where the law does not allow exculsion of implied warranties.
  260.  
  261.  
  262.  
  263. Bug reports make this a better product!
  264.  
  265. As in all of my products, I advertise a $10.00 finder's fee for bug reports that lead to corrections.  If you find a problem here, report it!  it could be worth your while....
  266.  
  267.  
  268.  
  269. Other
  270.  
  271. Have a good day,
  272.  
  273. John Montbriand
  274. Author
  275.  
  276.  
  277.